package com.bw.dao;

import com.bw.pojo.Customer;
import com.bw.util.JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***************************
 FileAction: 这个类提供对 数据库中 Customer表的增删改查方法的

 CreateTime:2022-05-07 14:39
 Author:Aaking-aline
 ****************************/
public class CustomerDao {

    // 查询所有客户信息的方法
    public List<Customer> selectAllCustomer() throws SQLException {
        String sql = "select * from customer";

        Connection conn = JDBC.getConnection();

        PreparedStatement ps = conn.prepareStatement(sql);

        ResultSet rs = ps.executeQuery();

        List<Customer> list = new ArrayList<>();

        while(rs.next()){
            Customer customer = new Customer(rs.getInt(1),
                    rs.getString(2),
                    rs.getInt(3),
                    rs.getString(4));

            list.add(customer);
        }

        return list;
    }


    public void deleteCustomerById(String id) throws SQLException {
        String sql = "delete from customer where id = ?";

        Connection conn = JDBC.getConnection();

        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setObject(1,id);

        ps.executeUpdate();
    }
}
